
package com.kujie.loader.vector;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

@Component
@Slf4j
public class DocxLoader implements ResourceLoader {

    @Override
    public String getContent(InputStream inputStream) {
        try (XWPFDocument document = new XWPFDocument(inputStream);
             XWPFWordExtractor extractor = new XWPFWordExtractor(document)) {
            String text = extractor.getText();
            return text != null ? text : "";
        } catch (IOException e) {
            log.error("解析 .docx 文件失败", e);
            throw new RuntimeException("解析 .docx 文件失败: " + e.getMessage(), e);
        }
    }
}